package com.todo.strategy.online.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.strategy.online.infrastructure.convert.MemoConvert;
import com.todo.strategy.online.infrastructure.entity.domain.MemoUserRelationDO;
import com.todo.strategy.online.infrastructure.entity.po.MemoUserRelationPO;
import com.todo.strategy.online.infrastructure.mapper.MemoUserRelationMapper;
import com.todo.strategy.online.infrastructure.repository.MemoUserRelationRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2024/11/29 00:08
 * @description
 */
@Slf4j
@Repository
public class MemoUserRelationRepositoryImpl implements MemoUserRelationRepository {

    @Resource
    private MemoUserRelationMapper memoUserRelationMapper;

    @Override
    public boolean insertMemoUserRelation(MemoUserRelationDO memoUserRelationDO) {
        if (memoUserRelationDO == null) {
            return false;
        }
        if (memoUserRelationDO.getUserId() == null || memoUserRelationDO.getPlanId() == null || memoUserRelationDO.getContent() == null) {
            log.error("MemoUserRelationRepository#insertMemoUserRelation 新增计划备忘录关联失败，数据异常 memoUserRelationDO：{}", JSONObject.toJSONString(memoUserRelationDO));
            return false;
        }
        MemoUserRelationPO memoUserRelationPO = MemoConvert.convertToMemoUserRelationPO(memoUserRelationDO);
        int insert = memoUserRelationMapper.insertMemoUserRelation(memoUserRelationPO);
        return insert > 0;
    }

    @Override
    public MemoUserRelationDO queryMemoUserRelationById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        MemoUserRelationPO memoUserRelationPO = memoUserRelationMapper.queryMemoUserRelationById(id);
        return MemoConvert.convertTomemoUserRelationDO(memoUserRelationPO);
    }

    @Override
    public MemoUserRelationDO queryMemoUserRelationByUserIdAndPlanId(Long userId, Long planId) {
        if (userId == null || planId == null) {
            return null;
        }
        MemoUserRelationPO memoUserRelationPO = memoUserRelationMapper.queryMemoUserRelationByUserIdAndPlanId(userId, planId);
        return MemoConvert.convertTomemoUserRelationDO(memoUserRelationPO);
    }

    @Override
    public boolean updateMemoUserRelationById(MemoUserRelationDO memoUserRelationDO) {
        if (memoUserRelationDO == null) {
            return false;
        }
        if (memoUserRelationDO.getContent() == null) {
            log.error("MemoUserRelationRepository#updateMemoUserRelationById 根据主键更新计划备忘录关联失败，数据异常 memoUserRelationDO：{}", JSONObject.toJSONString(memoUserRelationDO));
            return false;
        }
        MemoUserRelationPO memoUserRelationPO = MemoConvert.convertToMemoUserRelationPO(memoUserRelationDO);
        int update = memoUserRelationMapper.updateMemoUserRelationById(memoUserRelationPO);
        return update > 0;
    }

    @Override
    public boolean removeMemoUserRelationByUserIdAndPlanId(Long userId, Long planId) {
        if (userId == null || planId == null) {
            return false;
        }
        int remove = memoUserRelationMapper.removeMemoUserRelationByUserIdAndPlanId(userId, planId);
        return remove > 0;
    }

    @Override
    public boolean removeMemoUserRelationById(Long id) {
        if (id == null || id < 0) {
            return false;
        }
        int remove = memoUserRelationMapper.removeMemoUserRelationById(id);
        return remove > 0;
    }
}
